﻿var colorsay = new Object();
colorsay.GetQueryStringValueByName = function (key, defaultValue) {
    if (defaultValue == null) defaultValue = "";
    key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + key + "=([^&#]*)", "i");
    var qs = regex.exec(window.location.href);
    if (qs == null)
        return defaultValue;
    else
        return decodeURIComponent(qs[1]);
};

colorsay.IsOptionThree = function () {
    var optValue = colorsay.GetQueryStringValueByName("opt", "one");
    return optValue == "url";
};

colorsay.OpenNewWindowForReport = function (ck) {
    var sd = $("input[id$='StartDateTextBox']").val();
    var ed = $("input[id$='EndDateTextBox']").val();
    var selectedOpt = $("select[id$='ddlSelectedOption']").val();
    var analysisUrl = "";
    if (selectedOpt == "3") {
        var txtUrl = $("input[id$='txtSelectUrl']").val();
        if (txtUrl == "") {
            var selectedUrl = $("select[id$='ddlSelectUrl']").val();
            if (selectedUrl != "--- All ---") {
                analysisUrl = selectedUrl;
            }
        } else {
            analysisUrl = txtUrl;
        }
    }

    var afValue = "";
    var selectedAf = $("select[id$='ddlAttrsFamily']").val();
    if (selectedAf != null && selectedAf != "--- All ---") {
        afValue = $.trim(selectedAf.split('-')[0]);
    }

    if (selectedOpt == null) selectedOpt = "";
    
    var adminId = colorsay.GetQueryStringValueByName("id");
    var url = "http://" + window.location.host + "/color/ReportDetails.aspx?id=" + adminId + "&sd=" + sd + "&ed=" + ed + "&so=" + selectedOpt +
        "&au=" + analysisUrl + "&af=" + afValue + "&ck=" + ck;
    window.open(url, "_blank", "directories=no,titlebar=no,toolbar=no,location=no,scrollbars=yes,resizable=yes, top=200, left=200, width=800, height=600");
};

colorsay.SelectOptionChanged = function () {
    var sd = $("input[id$='StartDateTextBox']").val();
    var ed = $("input[id$='EndDateTextBox']").val();
    var selectedAf = $("select[id$='ddlAttrsFamily']").val();
    var selectedOpt = $("select[id$='ddlSelectedOption']").val();
    var adminId = colorsay.GetQueryStringValueByName("id");


    var su = "";
    if (selectedOpt == "3") {
        var specificUrl = $("input[id$='txtSelectUrl']").val();
        if (specificUrl != "") {
            su = specificUrl;
        } else {
            var ddlValue = $("select[id$='ddlSelectUrl']").val();
            if (ddlValue != "0") {
                su = ddlValue;
            }
        }
    }

    var url = "http://" + window.location.host + "/color/Reports.aspx?id=" + adminId + "&sd=" + sd + "&ed=" + ed +
        "&so=" + selectedOpt + "&af=" + selectedAf + "&su=" + su;
    location.href = url;
};
